<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //动态原型模式把所有信息都封装在了构造函数中，而通过在构造函数中初始化原型（
        function Student2 (name,age){
            this.name=name;
            this.age=age;
            this.girls=["久久","美杜莎"];
            if (typeof this.sayName !== "function") {
                Student2.prototype.sayName=function(){
                    console.log(this.name);
                }
            }
        }
        //注意如果 prototype 执行一个对象  就变化了
        //使用动态原型模式时，不能使用对象字面量重写原型。前面已经解释过了，
        //如果在已经创建了实例的情况下重写原型，
        //那么就会切断现有实例与新原型之间的联系
        function Student (name,age){
            this.name=name;
            this.age=age;
            this.girls=["久久","美杜莎"];
            if (typeof this.sayName !== "function") {
                Student.prototype={
                    constructor:Student,
                    sayName:function(){
                        console.log(this.name);
                    }
                    
                }
            }
        }
        var stu1=new Student2("小风",12);
        var stu2=new Student2("小宏",12);
        var stu3=new Student("小函数",12);
        var stu4=new Student("小开始",12);
        var stu5=new Student("小打始",12);
    </script>
</body>
</html>



